package com.dawn.usermanage.serviceimpl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import weibo4j.User;
import weibo4j.Weibo;
import weibo4j.WeiboException;

import com.dawn.usermanage.bean.SinaInfoBean;
import com.dawn.usermanage.bean.UserBean;
import com.dawn.usermanage.dao.SinaUserMDao;
import com.dawn.usermanage.service.SinaUserMService;
import com.dawn.usermanage.service.UserManageService;
import com.dawn.utils.exceptions.DaoAccessException;

@Service
public class SinaUserMServiceImpl implements SinaUserMService {

	private static Logger logger = Logger.getLogger(SinaUserMServiceImpl.class);

	@Autowired
	private SinaUserMDao sinaUserMDao;
	@Autowired
	private UserManageService userManageService;

	public SinaInfoBean findSUserInfo(String sinaId) {
		try {
			return sinaUserMDao.findSinaUser(sinaId);
		} catch (Exception e) {
			logger.error("Data access error");
			e.printStackTrace();
			throw new DaoAccessException();
		}
	}

	public boolean addUserInfo(UserBean userBean) {
		Calendar calendar = Calendar.getInstance();
		userBean.setCreateTime(calendar);
		userBean.setLastLoginTime(calendar);
		try {
			return userManageService.addUser(userBean);
		} catch (Exception e) {
			logger.error("Data access error");
			e.printStackTrace();
			throw new DaoAccessException();
		}
	}

	public boolean updateSinaBasic(String sinaId, Weibo weibo) {
		SinaInfoBean sinaUser = sinaUserToLocal(sinaId, weibo);
		try {
			sinaUserMDao.updateSinaBasic(sinaUser);
		} catch (Exception e) {
			logger.error("Data access error");
			e.printStackTrace();
			throw new DaoAccessException();
		}
		return true;
	}

	public SinaInfoBean addSinaBasic(String sinaId, Weibo weibo) {
		SinaInfoBean sinaUser = sinaUserToLocal(sinaId, weibo);
		try {
			sinaUserMDao.addSinaUser(sinaUser);
		} catch (Exception e) {
			logger.error("Data access error");
			e.printStackTrace();
			throw new DaoAccessException();
		}
		return sinaUser;
	}

	private SinaInfoBean sinaUserToLocal(String sinaId, Weibo weibo) {
		SinaInfoBean sinaUser = new SinaInfoBean();
		try {
			User user = weibo.showUser(sinaId);
			sinaUser.setSinaId(Long.toString(user.getId()));
			sinaUser.setScreenName(user.getName());
			sinaUser.setFansNum(user.getFollowersCount());
			sinaUser.setFollowNum(user.getFriendsCount());
			sinaUser.setStatusesCount(user.getStatusesCount());
			sinaUser.setGender(user.getGender());
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(user.getCreatedAt());
			sinaUser.setCreatedAt(calendar);
			sinaUser.setDescription(user.getDescription());
			sinaUser.setProfileBackgroundImageUrl(user.getProfileBackgroundImageUrl());
			sinaUser.setProfileBackgroundTile(user.getProfileBackgroundTile());
			sinaUser.setProfileImageUrl(user.getProfileImageURL() == null ? "" : user.getProfileImageURL().toString());
			sinaUser.setVerified(user.isVerified());
			sinaUser.setUrl(user.getURL() == null ? "" : user.getURL().toString());
		} catch (WeiboException e) {
			e.printStackTrace();
			return null;
		}
		return sinaUser;
	}
}
